﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Windows.Forms;

/*
 * 脚本功能：教师端
 *          开启UDP 屏幕共享
 */
namespace ScreenTranslate
{
    public partial class Teacher : Form
    {
        Server server;
        Image I;
        Graphics G;
        int port = 1237;
        int index = 0;

        public static string GetLocalIP()
        {
            try
            {
                string HostName = Dns.GetHostName();
                IPHostEntry IpEntry = Dns.GetHostEntry(HostName);
                for (int i = 0; i < IpEntry.AddressList.Length; i++)
                {
                    if (IpEntry.AddressList[i].AddressFamily == AddressFamily.InterNetwork)
                    {
                        return IpEntry.AddressList[i].ToString();
                    }
                }
                return "";
            }
            catch (Exception ex)
            {
                return ex.Message;
            }
        }

        public Teacher()
        {
            InitializeComponent();
            label1.Text = GetLocalIP();
        }

        private void Teacher_FormClosing(object sender, FormClosingEventArgs e)
        {
            //CloseServer();
            Center.Instance.ShowMainPanel();
        }

        private void StartServerBtn_Click(object sender, EventArgs e)
        {
            //最小化窗口
            this.WindowState = FormWindowState.Minimized;
            StartServer();
        }

        private void StopServer_Click(object sender, EventArgs e)
        {
            CloseServer();
        }

        /// <summary>
        /// 开启服务器
        /// </summary>
        private void StartServer()
        {
            //200毫秒广播一次屏幕数据
            timer1.Interval = 10;
            server = new Server(this,port);
            server.StartServer();
            //开始广播
            timer1.Start();
        }

        /// <summary>
        /// 关闭服务器
        /// </summary>
        private void CloseServer()
        {
            //关闭广播
            timer1.Stop();
            //关闭UDP连接
            if (server != null)
            {
                server.Close();
            }
        }

        /// <summary>
        /// 广播屏幕 开启服务器监听
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void timer1_Tick(object sender, EventArgs e)
        {
            //获取屏幕数据
            I = new Bitmap(Screen.PrimaryScreen.Bounds.Size.Width, Screen.PrimaryScreen.Bounds.Size.Height);
            G = Graphics.FromImage(I);
            G.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size);
            //I.Save("d://1.jpg", ImageFormat.Jpeg);
            pictureBox1.Image = I;
            //转换数据
            byte[] buf = ImageToBytes(I);
            //index++;
            //byte[] buf = Encoding.UTF8.GetBytes("心跳"+index);
            server.Send(buf);
        }
        
        /// <summary>
        /// 图片转换数据
        /// </summary>
        /// <param name="_image"></param>
        /// <returns></returns>
        private byte[] ImageToBytes(Image _image)
        {
            MemoryStream ms = new MemoryStream();
            _image.Save(ms, ImageFormat.Jpeg);
            return ms.ToArray();
        }

        private void label1_Click(object sender, EventArgs e)
        {

        }
    }
}
